package com.sinothk.hussars.controller;


import com.sinothk.hussars.base.BaseController;
import com.sinothk.hussars.entity.PermissionsEntity;
import com.sinothk.hussars.service.PermissionsService;
import com.sinothk.server.support.annotation.callback.BizException;
import com.sinothk.server.support.annotation.callback.ResponseResult;
import com.sinothk.server.support.utils.IdUtil;
import com.sinothk.server.support.utils.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 权限表 前端控制器
 * </p>
 *
 * @author liangyt
 * @since 2021-01-02
 */
@Api(tags = "权限_管理")
@RestController
@RequestMapping("/permissions")
@ResponseResult()
public class PermissionsController extends BaseController {

    private final PermissionsService permissionsService;

    public PermissionsController(PermissionsService permissionsService) {
        this.permissionsService = permissionsService;
    }

    @ApiOperation(value = "新增: 权限")
    @PostMapping("/save")
    public PermissionsEntity save(@RequestBody PermissionsEntity permissionsEntity) {
        // 参数验证
        if (StringUtil.isEmpty(permissionsEntity.getPsCode())) throw new BizException("权限编码不能为空");
        if (StringUtil.isEmpty(permissionsEntity.getPsName())) throw new BizException("权限名称不能为空");
        if (StringUtil.isEmpty(permissionsEntity.getPsInfo())) throw new BizException("权限说明不能为空");

        permissionsEntity.setId(IdUtil.getIncreaseIdByNanoTime());
        permissionsService.save(permissionsEntity);
        return permissionsService.getById(permissionsEntity.getId());
    }

    @ApiOperation(value = "更新：权限")
    @PostMapping("/update")
    public PermissionsEntity update(@RequestBody PermissionsEntity pmVo) {
        // 参数验证
        if (StringUtil.isEmpty(pmVo.getId())) throw new BizException("人员Id不能为空");
        permissionsService.updateById(pmVo);
        return permissionsService.getById(pmVo.getId());
    }

    @ApiOperation(value = "查询：权限列表")
    @PostMapping("/psList")
    public List<PermissionsEntity> psList() {
        return permissionsService.list();
    }
}


